<template>
  <el-form label-width="80px" :model="form" :rules="rules" ref="form">

      <el-form-item label="商品名称" prop="name">
        <el-input v-model="form.name"></el-input>
      </el-form-item>

      <el-form-item label="商品分类" prop="category">
        <el-select v-model="form.category">
          <el-option v-for="item in cateList" :key="item.id" :label="item.cateName" :value="item.cateName"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="商品价格" prop="price">
         <el-input-number v-model="form.price"></el-input-number>
      </el-form-item>

      <el-form-item label="商品图片" prop="imgUrl">
        <el-upload action="http://1.15.179.44:3001/goods/goods_img_upload" :on-success="success" :show-file-list="false">
          <img v-if="form.imgUrl" :src=" 'http://1.15.179.44:3001' + '/upload/imgs/goods_img/' + form.imgUrl" alt="" srcset="">
           <i v-else style="font-size: 58px" class="el-icon-plus"></i>
        </el-upload>
      </el-form-item>

      <el-form-item label="描述" prop="goodsDesc">
        <el-input v-model="form.goodsDesc"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button @click="submit">{{ type === 'add' ? '添加':'编辑'  }}商品</el-button>
      </el-form-item>
    </el-form>
</template>

<script>
import { catelistReq } from "@/api/goods.api";
export default {
  props: {
    // add（添加操作）   edit（编辑操作）
    type: {
      type: String,
      default: "add"
    },
    defaultForm: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      // 商品分类列表
      cateList: [],
      form: {
        name: "",
        category: "",
        price: "",
        imgUrl: "",
        goodsDesc: ""
      },
      rules: {
        name: { required: true, message: "请输入账号", trigger: "blur" },
        category: { required: true, message: "请选择分类", trigger: "change" },
        price: { required: true, message: "请输入价格", trigger: "blur" },
        imgUrl: { required: true, message: "请上传图片", trigger: "change" },
        goodsDesc: { required: true, message: "请输入秒速", trigger: "blur" }
      }
    };
  },
  methods: {
    // 商品图片上传成功
    success(res) {
      console.log(res);
      let imgUrl = res.imgUrl.split("/").pop();
      this.form.imgUrl = imgUrl;
      // 手动触发一次校验（只校验imgUrl）
      this.$refs.form.validateField("imgUrl");
    },
    submit() {
      // 校验以下表单： 1. 拿到表单对象 2.调用表单对象的校验方法
      this.$refs.form.validate(async res => {
        console.log("提交的时候校验表单，结果为：", res);
        if (res) {
          // 表单校验通过了
          this.$emit("success", this.form);
        } else {
          // 表单校验失败了
        }
      });
    }
  },
  // created只执行一次，
  async created() {
    const res = await catelistReq();
    this.cateList = res.data.data;
  },
  watch: {
    // 得到了defaultForm，把defaultForm里面的数据给data下的form（只有编辑的时候才走）
    // 只要点击编辑了，就要重新赋值。 只要defaultForm变了，就需要执行下列逻辑（update watch）
    defaultForm: {
      handler: function() {
        if (this.type === "edit") {
          console.log("开始初始化表单数据！！！", "watch");
          this.form = { ...this.defaultForm };
          // 对于图片我们需要特殊处理一下，只取文件名
          this.form.imgUrl = this.defaultForm.imgUrl.split("/").pop();
        }
      },
      immediate: true
    }
  }
};
</script>

<style lang="less" scoped>
</style>